package com.hzbank.aaronkotlin

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.permissionx.guolindev.PermissionX
import com.permissionx.guolindev.callback.RequestCallback

abstract class BaseActivity<out T: ViewBinding>: AppCompatActivity() {

    private lateinit var _binding: T

    val xBing: T?
        get() {
            try{

                if(this::_binding.isLateinit){

                    return this::_binding.get()
                }

                return null

            }catch (e: Exception){
                e.printStackTrace()
            }

            return null
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        _binding = getBinding()
        setContentView(_binding.root)

        parseView()

    }

    override fun onResume() {
        super.onResume()
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    abstract fun getBinding(): T
    abstract fun parseView()

    fun requestPermission(success: () -> Unit,
                          doDenied: (MutableList<String>) -> Unit,
                          vararg permissioms: String){

        PermissionX.init(this@BaseActivity)
            .permissions(
                *permissioms
            ).onExplainRequestReason{scope, deniedList ->

                scope.showRequestReasonDialog(deniedList,
                    "请授权必要权限",
                    "好的",
                    "取消")

            }.onForwardToSettings{scope, deniedList ->

                scope.showForwardToSettingsDialog(deniedList,
                    "你需要在设置中手动允许必要权限。",
                    "好的",
                    "取消")

            }
            .request{
                allGranted, grantedList, deniedList ->

                if(allGranted){

                    //全部通过授权
                    "权限全部通过".showLog()

                    success()

                } else {

                    //未授权
                    if(null!=deniedList
                        && !deniedList.isEmpty()){

                        //处理拒绝的权限逻辑
                        doDenied(deniedList)

                    }



                }
            }

    }

}